package com.ulook.lookplanet.ui.home;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.ulook.lookplanet.data.repository.HomeRepository;
import com.ulook.lookplanet.data.source.HomeDataSource;
import com.ulook.lookplanet.util.ThrowException;
import com.ulook.lookplanet.util.net.OkHttpException;
import com.ulook.lookplanet.util.net.ResponseCallback;

public class HomeViewModel extends ViewModel {

    private MutableLiveData<VUserInfo> userInfo = new MutableLiveData<>();
    private MutableLiveData<String> toastText = new MutableLiveData<>();
    private HomeRepository repository;

    public HomeViewModel() {
        super();
        repository = HomeRepository.getInstance(new HomeDataSource());
    }

    public LiveData<VUserInfo> getUserInfo() {
        return userInfo;
    }
    public LiveData<String> getToastText() {
        return toastText;
    }

    /**
     * 初始化页面展示的信息
     * @param userId
     */
    public void initUserInfo(int userId){
        // 游客
        if(userId == 0){
            VUserInfo vUserInfo = new VUserInfo();
            vUserInfo.setId(0);
            vUserInfo.setName("游客");
            userInfo.setValue(vUserInfo);
        // 用户
        }else {
            ResponseCallback callback = new ResponseCallback() {
                @Override
                public void onSuccess(Object res) {
                    userInfo.setValue((VUserInfo) res);
                }
                @Override
                public void onFailure(OkHttpException failuer) {
                    toastText.setValue("获取用户信息失败");
                    ThrowException.print(failuer, "获取用户信息失败"+failuer.getEmsg());
                }
            };
            repository.initUserInfo(userId, callback);
        }
    }

    public void logout(){
        VUserInfo vUserInfo = new VUserInfo();
        vUserInfo.setId(0);
        vUserInfo.setName("游客");
        userInfo.setValue(vUserInfo);
    }

}